<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{$is_post ? '正在播放' : 'M3U8高清播放器'}</title>
    <!-- 使用国内CDN资源 -->
    <link href="https://cdn.staticfile.org/video.js/7.20.1/video-js.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/animate.css/4.1.1/animate.min.css" rel="stylesheet">
    <script src="https://cdn.staticfile.org/video.js/7.20.1/video.min.js"></script>
    <script src="https://cdn.staticfile.org/videojs-contrib-hls/5.15.0/videojs-contrib-hls.min.js"></script>
    <style>
        :root {
            --primary-color: #1890ff;
            --primary-hover: #40a9ff;
            --success-color: #52c41a;
            --error-color: #ff4d4f;
            --warning-color: #faad14;
            --bg-color: #f0f2f5;
            --card-color: #ffffff;
            --text-color: #333;
            --text-secondary: #666;
            --border-color: #e8e8e8;
            --shadow: 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 9px 28px 8px rgba(0, 0, 0, 0.05);
        }
        
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: var(--bg-color);
            color: var(--text-color);
            line-height: 1.6;
        }
        
        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 15px;
        }
        
        .player-card {
            background: var(--card-color);
            border-radius: 12px;
            box-shadow: var(--shadow);
            overflow: hidden;
            transition: all 0.3s;
        }
        
        .player-header {
            padding: 18px 24px;
            background: linear-gradient(135deg, var(--primary-color), #096dd9);
            color: white;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .player-title {
            display: flex;
            align-items: center;
            font-size: 20px;
            font-weight: 500;
        }
        
        .player-title i {
            margin-right: 12px;
            font-size: 24px;
        }
        
        .player-actions {
            display: flex;
            gap: 10px;
        }
        
        .member-info {
            display: flex;
            align-items: center;
            margin-right: 15px;
            color: white;
        }
        
        .member-info i {
            margin-right: 5px;
        }
        
        .member-link {
            color: white;
            margin-left: 10px;
            opacity: 0.8;
            transition: opacity 0.3s;
        }
        
        .member-link:hover {
            opacity: 1;
            text-decoration: none;
        }
        
        .member-actions {
            display: flex;
            gap: 10px;
            margin-right: 15px;
        }
        
        .member-btn {
            padding: 6px 12px;
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            display: flex;
            align-items: center;
            transition: all 0.3s;
            text-decoration: none;
        }
        
        .member-btn:hover {
            background: rgba(255, 255, 255, 0.3);
            color: white;
        }
        
        .member-btn i {
            margin-right: 6px;
        }
        
        .player-btn {
            padding: 6px 12px;
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            display: flex;
            align-items: center;
            transition: all 0.3s;
        }
        
        .player-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .player-btn i {
            margin-right: 6px;
        }
        
        .player-body {
            padding: 24px;
        }
        
        .url-form {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        
        .url-input-container {
            flex: 1;
            position: relative;
        }
        
        .url-input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            font-size: 15px;
            transition: all 0.3s;
            padding-right: 40px;
        }
        
        .url-input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
        }
        
        .url-input.error {
            border-color: var(--error-color);
        }
        
        .clear-input {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-secondary);
            cursor: pointer;
            display: none;
        }
        
        .play-button {
            padding: 0 20px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 15px;
            font-weight: 500;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            min-width: 100px;
        }
        
        .play-button:hover {
            background-color: var(--primary-hover);
        }
        
        .play-button i {
            margin-right: 8px;
        }
        
        .video-container {
            margin-bottom: 20px;
            border-radius: 8px;
            overflow: hidden;
            background: #000;
            position: relative;
            min-height: 300px;
        }
        
        .video-placeholder {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100%;
            color: #999;
            padding: 40px;
            text-align: center;
        }
        
        .video-placeholder i {
            font-size: 48px;
            margin-bottom: 20px;
            opacity: 0.6;
        }
        
        .video-placeholder p {
            max-width: 400px;
            margin: 0 auto;
        }
        
        .panel-container {
            display: flex;
            gap: 20px;
            margin-top: 20px;
        }
        
        .panel {
            flex: 1;
            background: var(--card-color);
            border-radius: 8px;
            padding: 16px;
            box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.03);
        }
        
        .panel-title {
            font-size: 16px;
            font-weight: 500;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
        }
        
        .panel-title i {
            margin-right: 8px;
            color: var(--primary-color);
        }
        
        .history-list {
            list-style: none;
        }
        
        .history-item {
            padding: 10px 0;
            border-bottom: 1px dashed var(--border-color);
            display: flex;
            justify-content: space-between;
        }
        
        .history-item:last-child {
            border-bottom: none;
        }
        
        .history-url {
            flex: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            margin-right: 10px;
            color: var(--text-color);
            text-decoration: none;
        }
        
        .history-url:hover {
            color: var(--primary-color);
        }
        
        .history-time {
            color: var(--text-secondary);
            font-size: 13px;
        }
        
        .error-message {
            color: var(--error-color);
            font-size: 13px;
            margin-top: 5px;
            display: none;
        }
        
        .status-bar {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            font-size: 13px;
            color: var(--text-secondary);
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 0 10px;
            }
            
            .player-header {
                padding: 12px 15px;
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }
            
            .player-actions {
                width: 100%;
                justify-content: flex-end;
            }
            
            .player-body {
                padding: 15px;
            }
            
            .url-form {
                flex-direction: column;
            }
            
            .panel-container {
                flex-direction: column;
            }
            
            .video-container .vjs-fluid {
                padding-top: 56.25% !important;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="player-card animate__animated animate__fadeIn">
            <div class="player-header">
                <div class="player-title">
                    <i class="fas fa-tv"></i>
                    <span>M3U8高清播放器</span>
                </div>
                <div class="player-actions">
                    {if $is_login}
                    <div class="member-info">
                        <i class="fas fa-user"></i>
                        <span>{$member_name}</span>
                        <a href="{:url('member/center')}" class="member-link" title="会员中心">
                            <i class="fas fa-user-cog"></i>
                        </a>
                        <a href="{:url('member/logout')}" class="member-link" title="退出登录">
                            <i class="fas fa-sign-out-alt"></i>
                        </a>
                    </div>
                    {else}
                    <div class="member-actions">
                        <a href="{:url('member/login')}" class="member-btn">
                            <i class="fas fa-sign-in-alt"></i>
                            <span>登录</span>
                        </a>
                        <a href="{:url('member/register')}" class="member-btn">
                            <i class="fas fa-user-plus"></i>
                            <span>注册</span>
                        </a>
                    </div>
                    {/if}
                    <button class="player-btn" id="fullscreenBtn">
                        <i class="fas fa-expand"></i>
                        <span>全屏</span>
                    </button>
                    <button class="player-btn" id="refreshBtn">
                        <i class="fas fa-sync-alt"></i>
                        <span>刷新</span>
                    </button>
                </div>
            </div>
            
            <div class="player-body">
                <form id="m3u8Form" method="post" class="url-form">
                    <input type="hidden" name="__token__" value="{:token()}">
                    <div class="url-input-container">
                        <input type="text" name="url" id="urlInput" class="url-input" placeholder="请输入M3U8视频地址 (例如: https://example.com/video.m3u8)" value="{$url}" required>
                        <i class="fas fa-times clear-input" id="clearInput"></i>
                    </div>
                    <button type="submit" class="play-button">
                        <i class="fas fa-play"></i> 播放
                    </button>
                    <div id="errorMsg" class="error-message"></div>
                </form>
                
                <div class="video-container">
                    {if $is_post}
                    <video-js id="my-player" class="video-js vjs-fluid vjs-big-play-centered" controls preload="auto" poster="https://cdn.staticfile.org/video.js/7.20.1/examples/img/poster.jpg">
                        <source src="{$url}" type="application/x-mpegURL">
                        <p class="vjs-no-js">
                            您的浏览器不支持HTML5视频，请使用现代浏览器如Chrome、Firefox或Edge。
                        </p>
                    </video-js>
                    {else}
                    <div class="video-placeholder">
                        <i class="fas fa-film"></i>
                        <h3>欢迎使用M3U8播放器</h3>
                        <p>请输入M3U8视频地址开始播放，支持HLS协议的直播和点播流媒体</p>
                    </div>
                    {/if}
                </div>
                
                <div class="panel-container">
                    <div class="panel">
                        <h3 class="panel-title"><i class="fas fa-info-circle"></i> 使用说明</h3>
                        <ul style="padding-left: 20px;">
                            <li>支持标准的M3U8格式视频流</li>
                            <li>可播放直播流和点播视频</li>
                            <li>自动适配不同清晰度(如果有多个码率)</li>
                            <li>全屏模式提供更好的观看体验</li>
                            {if $is_login}
                            <li>登录后可自动记录观看历史</li>
                            {else}
                            <li><a href="{:url('member/login')}" style="color: var(--primary-color);">登录</a>后可记录观看历史</li>
                            {/if}
                        </ul>
                    </div>
                    
                    {if !empty($history)}
                    <div class="panel">
                        <h3 class="panel-title">
                            <i class="fas fa-history"></i>
                            {if $is_login}我的{/if}播放历史
                        </h3>
                        <ul class="history-list">
                            {foreach $history as $item}
                            <li class="history-item">
                                <a href="javascript:;" class="history-url" onclick="playHistory('{$item.video_url}')">
                                    {$item.title ?: $item.video_url|mb_substr=0,50}
                                </a>
                                <span class="history-time">{$item.created_at|date='m-d H:i'}</span>
                            </li>
                            {/foreach}
                        </ul>
                    </div>
                    {/if}
                </div>
                
                <div class="status-bar">
                    <div>
                        <i class="fas fa-check-circle" style="color: var(--success-color);"></i>
                        <span>播放器已就绪</span>
                    </div>
                    <div>
                        <span>ThinkPHP 6.1 + Video.js</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {if $is_post}
    <script>
        // 初始化播放器
        var player = videojs('my-player', {
            responsive: true,
            fluid: true,
            controlBar: {
                volumePanel: {
                    inline: false
                },
                children: [
                    'playToggle',
                    'progressControl',
                    'volumePanel',
                    'currentTimeDisplay',
                    'timeDivider',
                    'durationDisplay',
                    'qualitySelector',
                    'pictureInPictureToggle',
                    'fullscreenToggle'
                ]
            }
        });
        
        // 错误处理
        player.ready(function() {
            this.on('error', function() {
                showError(this.error());
            });
        });
        
        function showError(error) {
            var errorMessages = {
                1: '视频加载过程中发生错误，请检查网络连接',
                2: '视频格式不支持或网络问题导致加载失败',
                3: '视频解码错误，可能是编码格式问题',
                4: '视频无法解密，可能是加密流媒体',
                5: '视频源不支持，请尝试其他地址'
            };
            
            var errorMsg = errorMessages[error.code] || '播放发生未知错误，请检查视频地址';
            document.getElementById('errorMsg').textContent = errorMsg;
            document.getElementById('errorMsg').style.display = 'block';
            document.getElementById('urlInput').classList.add('error');
        }
        
        {if $is_login}
        // 记录观看进度
        var timer;
        var lastProgress = 0;
        
        player.ready(function() {
            this.on('timeupdate', function() {
                var currentTime = Math.floor(this.currentTime());
                
                // 只有时间变化超过5秒才记录
                if (Math.abs(currentTime - lastProgress) >= 5) {
                    clearTimeout(timer);
                    timer = setTimeout(function() {
                        recordProgress(currentTime);
                        lastProgress = currentTime;
                    }, 1000);
                }
            });
            
            this.on('ended', function() {
                recordProgress(Math.floor(player.duration()));
            });
        });
        
        function recordProgress(progress) {
            fetch('{:url("player/recordProgress")}', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': document.querySelector('input[name="__token__"]').value
                },
                body: JSON.stringify({
                    url: '{$url}',
                    progress: progress,
                    duration: Math.floor(player.duration())
                })
            });
        }
        {/if}
    </script>
    {/if}
    
    <script>
        // DOM加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            // 输入框交互
            var urlInput = document.getElementById('urlInput');
            var clearInput = document.getElementById('clearInput');
            
            urlInput.addEventListener('input', function() {
                clearInput.style.display = this.value ? 'block' : 'none';
                this.classList.remove('error');
                document.getElementById('errorMsg').style.display = 'none';
            });
            
            clearInput.addEventListener('click', function() {
                urlInput.value = '';
                this.style.display = 'none';
                urlInput.focus();
            });
            
            // 表单验证
            document.getElementById('m3u8Form').addEventListener('submit', function(e) {
                var errorMsg = document.getElementById('errorMsg');
                errorMsg.style.display = 'none';
                urlInput.classList.remove('error');
                
                if (!urlInput.value.trim()) {
                    e.preventDefault();
                    errorMsg.textContent = '请输入M3U8视频地址';
                    errorMsg.style.display = 'block';
                    urlInput.classList.add('error');
                    urlInput.focus();
                    return;
                }
                
                if (!urlInput.value.match(/^https?:\/\/.+\..+/) || !urlInput.value.includes('.m3u8')) {
                    e.preventDefault();
                    errorMsg.textContent = '请输入有效的M3U8地址（以http/https开头，包含.m3u8）';
                    errorMsg.style.display = 'block';
                    urlInput.classList.add('error');
                    urlInput.focus();
                }
            });
            
            // 播放历史点击
            window.playHistory = function(url) {
                document.getElementById('urlInput').value = url;
                document.getElementById('m3u8Form').submit();
            };
            
            // 全屏按钮
            document.getElementById('fullscreenBtn').addEventListener('click', function() {
                if (document.fullscreenEnabled) {
                    if (!document.fullscreenElement) {
                        document.documentElement.requestFullscreen().catch(err => {
                            alert('全屏模式错误: ' + err.message);
                        });
                    } else {
                        document.exitFullscreen();
                    }
                } else {
                    alert('您的浏览器不支持全屏模式');
                }
            });
            
            // 刷新按钮
            document.getElementById('refreshBtn').addEventListener('click', function() {
                window.location.reload();
            });
        });
    </script>
</body>
</html>